package com.csnf.controller;

import com.csnf.service.GoodsService;
import csnf.com.entity.Goods;
import csnf.com.entity.ResponseObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Map;

@RestController

@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;
    @PostMapping("/add")
    public ResponseObject addGoods(@RequestBody Map<String, Object> payload) {
        Goods goods = new Goods();
        goods.setGName((String) payload.get("gName"));
        goods.setGPrice(Double.valueOf(payload.get("gPrice").toString()));
        goods.setGImg((String) payload.get("gImg"));
        goods.setGCount(Integer.parseInt(payload.get("gCount").toString()));
        goods.setIsSale(Integer.parseInt(payload.get("isSale").toString()));
        goods.setId(Integer.parseInt(payload.get("id").toString())); // 获取用户ID
        System.out.println("接收到的商品数据：" + goods);
        int result = goodsService.insert(goods);
        if (result > 0) {
            return ResponseObject.success("添加成功");
        } else {
            return ResponseObject.error("添加失败");
        }
    }
    @RequestMapping("/selectAll")
    public ResponseObject selectAll(@RequestParam(required = false) Integer id) {
        ArrayList<Goods> goods = goodsService.selectAll(id);
        if (goods.size()>0){
            return ResponseObject.success("查询成功",goods);
        }else {
            return ResponseObject.success("查询失败");
        }

    }
    @RequestMapping("/updateSaleStatus")
    public ResponseObject updateSaleStatus(@RequestBody Goods goods) {
        int result = goodsService.updateSaleStatus(goods);
        if (result > 0){
            return ResponseObject.success("修改成功");
        }else {
            return ResponseObject.success("修改失败");
        }
    }

}
